<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2020-02-04 22:57:38
 * Desc:
 */

namespace app\admin\controller\logistics;

use app\admin\controller\adminAuthController;
use app\base\model\address\UserPostInfo;
use app\base\model\goods\GoodsGroupInfo;
use app\base\model\user\User;
use app\core\exception\ErrorCode;
use app\core\log\Log4dd;
use think\Request;

class LogisticsController extends adminAuthController
{

    public function getLogisticsList()
    {
        $size = input('page_size/d', 20);
        $page = input('page/d', 1);
        $where = [];

        $post_state = input('post_state/d', -1);
        if ($post_state >= 0) {
            $where['post.post_state'] = $post_state;
        }

        $is_robot = input('is_robot/d', 0);
        if ($is_robot) {
            $where['user.account_type'] = User::ACCOUNT_TYPE_ROBOT;
        }

        $goods_id = input('goods_id/d', 0);
        if ($goods_id) {
            $where['goods.goods_id'] = $goods_id;
        }

        $goods_name = input('goods_name/s', '');
        if ($goods_name) {
            $where['goods.goods_name'] = ['like', "%$goods_name%"];
        }

        $search_num = input('search_num/s', '');

        $query_string = '';

        //group_id user_id 快递单号
        if ($search_num) {
            $query_string = "group.group_id = {$search_num} or user.user_id = {$search_num} or post.post_order = {$search_num}";
        }

        $start_time = input('start_time/s', '');
        $end_time = input('end_time/s', '');
        if ($start_time && $end_time) {
            $where['group.success_time'] = ['BETWEEN', [$start_time, $end_time]];;
        } elseif ($start_time) {
            $where['group.success_time'] = ['>=', $start_time];
        } elseif ($end_time) {
            $where['group.success_time'] = ['<=', $end_time];
        }


        $data = UserPostInfo::AdminGetLogisticsList($where, $query_string, $page, $size);
        return $this->successReturn($data);
    }


    public function fillLogisticsInfo()
    {
        $log_id = input('log_id/d', 0);

        if (empty($log_id)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '记录id为空');
        }

        $post_info = UserPostInfo::find($log_id);

        if (empty($post_info)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '记录不存在');
        }

        $post_company_name = input('post_company_name/s', '');
        $post_order = input('post_order/s', '');
        $post_time = input('post_time/s', '');
        $remark = input('remark/s', '');

        if (empty($post_company_name)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '快递公司为空');
        }

        if (empty($post_order)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '快递单号为空');
        }

        $data['post_company_name'] = $post_company_name;
        $data['post_order'] = $post_order;
        if (empty($post_info['post_time'])) {
            $data['post_time'] = $post_time ?  date('Y-m-d H:i:s', strtotime($post_time)) : date('Y-m-d H:i:s');
        }

        $remark && $data['remark'] = $remark;
        if ($post_info['post_state'] < 3 && $post_company_name && $post_order) {
            $data['post_state'] = 3;
        }

        $data['admin_name'] = $this->adminInfo['admin_name'];

        Log4dd::info('填写物流信息', [
            'admin_id' => $this->adminId,
            'admin_name' => $this->adminInfo['admin_name'],
            'params' => Request::instance()->param(false),
            'data' => $data
        ]);



        $ret = UserPostInfo::where('log_id', $log_id)->update($data);
        //订单发货提醒
        if ($ret && $post_info['post_state'] < 3 && $post_company_name && $post_order && $post_info['group_id']) {
            //根据拼团信息获取商品信息
            $goods_name = GoodsGroupInfo::getGoodsNameBYGroupId($post_info['group_id']);
            \app\base\service\WeChat\MiniProgramSubscribeMessageService::instance()->sendLogisticsNoticeMsg([
                'group_id' => $post_info['group_id'],
                'first' => config('wish_num_prefix') . $post_info['group_id'],
                'keyword1' => $goods_name,
                'keyword2' => $post_company_name,
                'keyword3' => $post_order,
                'keyword4' => '你发起的好物心愿已发货！点击可查看详情'
            ], $post_info['user_id']);
        }

        return $ret ? $this->successReturn() : $this->errorReturn();
    }

    public function updatePostState()
    {
        $where = [];

        $post_state = input('post_state/d', 1);
        if ($post_state >= 0) {
            $where['post.post_state'] = $post_state;
        }

        $is_robot = input('is_robot/d', 0);
        if ($is_robot) {
            $where['user.account_type'] = User::ACCOUNT_TYPE_ROBOT;
        }

        $goods_id = input('goods_id/d', 0);
        if ($goods_id) {
            $where['goods.goods_id'] = $goods_id;
        }

        $goods_name = input('goods_name/s', '');
        if ($goods_name) {
            $where['goods.goods_name'] = ['like', "%$goods_name%"];
        }

        $search_num = input('search_num/s', '');

        $query_string = '';

        //group_id user_id 快递单号
        if ($search_num) {
            $query_string = "group.group_id = {$search_num} or user.user_id = {$search_num} or post.post_order = {$search_num}";
        }

        $start_time = input('start_time/s', '');

        if ($goods_id) {
            $where['post.create_time'] = ['>=', $start_time];
        }

        $end_time = input('end_time/s', '');

        if ($end_time) {
            $where['post.create_time'] = ['<=', $end_time];
        }


        $ids = UserPostInfo::getAllPostLogIds($where, $query_string);

        if (empty($ids)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '没有符合条件的数据');
        }

        Log4dd::info('修改物流状态',
            [
                'admin_id' => $this->adminId,
                'admin_name' => $this->adminInfo['admin_name'],
                'params' => Request::instance()->param(false),
                'ids' => $ids,
                'state' => 2
            ]);

        $ret = UserPostInfo::thirdPlatformPostState($ids, 2);

        return $ret ? $this->successReturn() : $this->errorReturn();
    }

}